﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace PTCScheduler.FileMenu_Pages
{
    /// <summary>
    /// Interaction logic for Add_TeacherPage.xaml
    /// </summary>
    public partial class Add_TeacherPage : Page
    {
        public Add_TeacherPage()
        {
            InitializeComponent();
        }

        //Method to clear the textfields of new teacher form.
        public void ClearForm()
        {
            //Clear text fields.
            txtTeacherFName.Clear();
            txtTeacherLName.Clear();
            txtTeacherEmail.Clear();

            //Refocus to teacher first name textbox.
            txtTeacherFName.Focus();
        }

        //Method to hide all labels.
        public void HideAllLabels()
        {
            //Hide Labels.
            lblTeacherSuccess.Visibility = Visibility.Hidden;
            lblValidEmail.Visibility = Visibility.Hidden;
            lblProvideAllInfo.Visibility = Visibility.Hidden;
            lblTeacherExists.Visibility = Visibility.Hidden;
            lblValidName.Visibility = Visibility.Hidden;

            //Empty datagrid
            dataGridTeacher.ItemsSource = null;
        }

        //Method to check for valid email.
        public Boolean validEmail(String email)
        {
            if (email.Contains("@") && email.Contains("."))
            {
                return true;
            }
            return false;
        }

        //Handles the submit button being clicked to add new teacher.
        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            //Hide all labels.
            HideAllLabels();

            try
            {
                //Check to ensure teacher first name and last name is entered.
                if (txtTeacherFName.Text == String.Empty || txtTeacherLName.Text == String.Empty)
                {
                    throw new MissingFieldException();
                }
                //Check to ensure the teacher's name contains no numbers
                //if (!Regex.IsMatch(txtTeacherFName.Text, @"^[a-zA-Z]+$") || !Regex.IsMatch(txtTeacherLName.Text, @"^[a-zA-Z]+$"))
                //{
                    //throw new ArgumentException();
                //}

                //Create new teacher object
                Teacher teacher = new Teacher(txtTeacherFName.Text, txtTeacherLName.Text, txtTeacherEmail.Text);

                //Check for valid email and throw exception if not valid.
                if (!validEmail(txtTeacherEmail.Text))
                {
                    throw new FormatException();
                }
                else//If valid add teacher.
                {
                    //Try catch to ensure no existing teacher is added. Catches SQLite exception.
                    try
                    {
                        //Add New teacher.
                        teacher.AddNewTeacher();

                        //Display teacher in datagrid.
                        dataGridTeacher.ItemsSource = teacher.dvTeacher();

                        //Prompt user teacher was added successfully.
                        lblTeacherSuccess.Visibility = Visibility.Visible;

                        //Clear the form.
                        ClearForm();
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        //Display teacher exists label.
                        lblTeacherExists.Visibility = Visibility.Visible;

                        //Display existing teacher in datagrid.
                        dataGridTeacher.ItemsSource = teacher.dvTeacher();

                        //Clear form.
                        ClearForm();
                    }//End try catch to catch sqlite exception
                }
            }
            catch (Exception ex)
            {
                if (ex is FormatException)
                {
                    //Display invalid email label.
                    lblValidEmail.Visibility = Visibility.Visible;
                }
                else if (ex is MissingFieldException)
                {
                    //Display input all information label.
                    lblProvideAllInfo.Visibility = Visibility.Visible;
                }
                else if(ex is ArgumentException)
                {
                    lblValidName.Visibility = Visibility.Visible;
                }
            }
        }//End Submit Button Click Event Handler
    }
}
